package com.ruoyi.auth.controller;

import javax.servlet.http.HttpServletRequest;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.constant.SessionConstant;
import com.ruoyi.common.security.model.AccessToken;
import com.ruoyi.common.security.model.UserSession;
import com.ruoyi.common.security.utils.SaTokenUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.auth.form.LoginBody;
import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;

import java.util.Map;


/**
 * token 控制
 * 
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        AccessToken accessToken = new AccessToken();
        accessToken.setAccess_token(userInfo.getToken());
        accessToken.setExpires_in(userInfo.getExpireTime());
        // 获取登录token
        return R.ok(accessToken);
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        try{
            UserSession userSession = StpUtil.getSession().getModel(SessionConstant.USER_SESSION_KEY, UserSession.class);
//           String username = JwtUtils.getUserName(token);
//           // 删除用户缓存记录
//           AuthUtil.logoutByToken(token);
            StpUtil.logout();
            sysLoginService.logout(userSession.getUsername());
        }catch (NotLoginException e){
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
}
